var e = require,
    t = module,
    n = exports;

Object.defineProperty(n, "__esModule", {
    value: !0
}), n.PlayType = n.MatchType = n.GameState = void 0;

var o, i, a, r = e("Global"),
    s = e("GameModel"),
    c = e("BaseView"),
    l = e("AccountView"),
    d = e("RecorderManager"),
    u = e("PlayerModel"),
    p = e("BattleAccountView"),
    h = e("GameConfig"),
    f = e("GameData"),
    g = e("EquipManager"),
    m = e("EggsManager"),
    y = e("ActivityManager"),
    v = e("DataManager"),
    w = e("UIManager"),
    b = e("WebManager");

(function(t) {
    t[t["匹配"] = 0] = "匹配", t[t["战斗开始"] = 1] = "战斗开始", t[t["战斗中"] = 2] = "战斗中", t[t["战斗结束"] = 3] = "战斗结束",
        t[t["结算"] = 4] = "结算", t[t["其他"] = 5] = "其他";
})(o = n.GameState || (n.GameState = {})),
function(t) {
    t[t["正常"] = 0] = "正常", t[t["引导"] = 1] = "引导", t[t["三对三"] = 2] = "三对三";
}(i = n.MatchType || (n.MatchType = {})),
function(t) {
    t[t["道具"] = 0] = "道具", t[t["圣诞"] = 1] = "圣诞", t[t["元旦"] = 2] = "元旦";
}(a = n.PlayType || (n.PlayType = {}));

var P = function() {
    function G() {
        this.state = o.其他, this.model = new s.default(), this.level = 0, this.gameView = null,
            this.game2View = null, this.wincount = 20, this.aicount = 5, this.petcount = 1,
            this.petscale = .5, this.petlevel = [], this.machinecount = 1, this.machinescale = .05,
            this.eggGun = [], this.equipminlv = 0, this.equipmaxlv = 0, this.equipscale = 0,
            this.fieldgird = 3, this.maximum = 0, this.zoomratio = .8, this.bgPath = "0", this.jsonPath = "json/1",
            this.nums = [5, 4, 2, 2], this.restartcount = 0, this.matchType = i.正常, this.playTypeList = [];
    }
    return G.getInstance = function() {
        return this._manager || (this._manager = new G()), this._manager;
    }, G.prototype.matchGame = function() {
        var e, n = this;
        this.state == o.其他 && (this.state = o.匹配, e = Math.max(0, Math.floor(f.GD.level / 3)),
            this.aicount = 3, this.petcount = 0, this.maximum = 0, h.GC.aiConfig.forEach(function(t) {
                t.r <= e && (n.aicount = t.n);
            }), h.GC.petConfig.forEach(function(t) {
                t.r <= e && (n.petcount = t.n, n.petscale = t.b / 100, t.l && (n.petlevel = t.l));
            }), h.GC.machineConfig.forEach(function(t) {
                t.r <= e && (n.machinecount = t.n, n.machinescale = t.b / 100);
            }), h.GC.eggGunRankLimit.forEach(function(t) {
                t.rank <= e && -2 != m.default.getInstance().checkEggGun(t.id) && Math.random() < .1 && n.eggGun.push(t.id + 1e3);
            }), h.GC.equipAIConfigList.forEach(function(t) {
                t.rank <= e && (n.equipminlv = t.minLv, n.equipmaxlv = t.maxLv, n.equipscale = t.scale);
            }), this.getLevel(), this.model.curPlayer = new u.default(), this.model.curPlayer.openid = GA.Info.openId,
            this.model.curPlayer.personIdx = f.GD.personIdx, this.model.curPlayer.gunIdx = f.GD.gunIdx,
            this.model.curPlayer.backEquipIdx = f.GD.backEquipIdx, this.model.curPlayer.headEquipIdx = f.GD.headEquipIdx,
            this.model.curPlayer.equipDataList = f.GD.equipDataList, this.model.curPlayer.icon = f.GD.playerData.icon,
            this.model.curPlayer.nickname = f.GD.playerData.nickname, this.model.curPlayer.group = 0,
            this.initAI(this.level), this.model.ais.forEach(function(t, e) {
                t.group = e + 100;
            }), this.getPlayType(), this.doMatch());
    }, G.prototype.battleGame = function() {
        var e, n = this;
        this.state == o.其他 && (this.state = o.匹配, e = Math.max(0, Math.floor(f.GD.level / 3)),
            this.aicount = 5, this.petcount = 0, this.maximum = 0, h.GC.petConfig.forEach(function(t) {
                t.r <= e && (n.petcount = t.n, n.petscale = t.b / 100, t.l && (n.petlevel = t.l));
            }), h.GC.equipAIConfigList.forEach(function(t) {
                t.rank <= e && (n.equipminlv = t.minLv, n.equipmaxlv = t.maxLv, n.equipscale = t.scale);
            }), this.model.curPlayer = new u.default(), this.model.curPlayer.openid = GA.Info.openId,
            this.model.curPlayer.personIdx = f.GD.personIdx, this.model.curPlayer.gunIdx = f.GD.gunIdx,
            this.model.curPlayer.backEquipIdx = f.GD.backEquipIdx, this.model.curPlayer.headEquipIdx = f.GD.headEquipIdx,
            this.model.curPlayer.equipDataList = f.GD.equipDataList, this.model.curPlayer.icon = f.GD.playerData.icon,
            this.model.curPlayer.nickname = f.GD.playerData.nickname, this.model.curPlayer.group = 0,
            this.initBattleAI(r.G.wincount), this.model.ais.forEach(function(t, e) {
                t.group = e < 2 ? 0 : 100;
            }), this.getPlayType(), this.doMatch());
    }, G.prototype.gideGame = function() {
        if (this.state == o.其他) {
            this.state = o.匹配, this.aicount = 3, this.model.ais = [];
            for (var t = [3, 8, 9], e = [4, 6, 8], n = 0; n < G.getInstance().aicount; n++) {
                var a = new u.default();
                a.openid = r.G.getUuid(), a.personIdx = t[n % t.length], a.gunIdx = e[n % e.length],
                    a.think = 1, a.group = n + 100, this.model.ais.push(a);
            }
            this.doMatch("texture/map/bg/0", "json/0");
        }
    }, G.prototype.doMatch = function(t, e) {
        var n = this;
        this.getMap(t, e), w.default.getInstance().loadPnl("game2", null, function() {
            w.default.getInstance().hideAllPnl();
        }).then(function(t) {
            t instanceof c.default && (n.game2View = t);
        });
    }, G.prototype.doStart = function() {
        if (this.state == o.匹配) {
            this.state = o.战斗开始, G.getInstance().restartcount = 0;
            var t = w.default.getInstance().getPnl("start");
            if (t && (t.bgView.node.active = !1), this.gameView.doStart(), r.G.isFrist) {
                b.default.getInstance().sentLog("HomeStartGame");
                try {
                    GA.AdTracking.reportKeyAction(GA.AdTracking.Action.Register);
                } catch (t) {}
                r.G.isFrist = !1;
            }
            b.default.getInstance().sentLog("PlayGame");
        }
    }, G.prototype.startGame = function() {
        (this.state = o.战斗开始) && (this.state = o.战斗中, this.gameView && (this.gameView.startGame(),
            d.default.getInstance().start(90), d.default.getInstance().isRecorder = !1, GA.isPlatform([GA.Platform.ByteDance, GA.Platform.Kwaigame]) && d.default.getInstance().screenShot(this.gameView.followCamera)));
    }, G.prototype.loseGame = function() {
        this.gameView && this.gameView.loseGame();
    }, G.prototype.endGame = function() {
        if (this.state = o.战斗中) {
            if (this.state = o.战斗结束, this.gameView) {
                this.gameView.endGame();
                try {
                    d.default.getInstance().callback = function() {
                        var t = w.default.getInstance().getPnl("account");
                        t && t.showRecorder();
                    }, d.default.getInstance().stop();
                } catch (t) {}
            }
            this.matchType == i.三对三 ? w.default.getInstance().preloadPnl("battleAccount") : w.default.getInstance().preloadPnl("account");
        }
    }, G.prototype.visitGame = function() {
        this.gameView && this.gameView.visitGame();
    }, G.prototype.checkGame = function() {
        var e = this;
        if (this.gameView) {
            var n = this.model.players.filter(function(t) {
                return !t.isDead();
            });
            if (this.matchType == i.三对三) {
                if (0 < n.length && (n.every(function(t) {
                        return t.group == e.model.curPlayer.group;
                    }) || n.every(function(t) {
                        return t.group != e.model.curPlayer.group;
                    }))) return this.model.players.forEach(function(t) {
                    t.group == n[0].group && (t.is_win = 1, t.rank = 0);
                }), !0;
            } else if (1 == n.length) return n[0].is_win = 1, !(n[0].rank = 0);
        }
        return !1;
    }, G.prototype.accountGame = function() {
        var t, e = this;
        this.state == o.战斗结束 && (this.state = o.结算, this.gameView && this.gameView.accountGame(),
            (t = w.default.getInstance().getPnl("start")) && (t.bgView.node.active = !0), this.matchType == i.三对三 ? w.default.getInstance().loadPnl("battleAccount", null, function() {
                e.gameView && e.gameView.hidePnl(), e.game2View && e.game2View.hidePnl();
            }).then(function(t) {
                t instanceof c.default && (t.getComponent(p.default).init(), (GA.isWeChat() || GA.isQQ()) && v.default.getInstance().reqRank("wqycj"),
                    b.default.getInstance().sentLog("GameEnd", f.GD.level));
            }) : w.default.getInstance().loadPnl("account", null, function() {
                e.gameView && e.gameView.hidePnl(), e.game2View && e.game2View.hidePnl();
            }).then(function(t) {
                t instanceof c.default && ((t = t.getComponent(l.default)).init(e.model.players.filter(function(t) {
                        return t.openid == GA.Info.openId;
                    })[0]), t.refresh(), (GA.isWeChat() || GA.isQQ()) && v.default.getInstance().reqRank("wqycj"),
                    b.default.getInstance().sentLog("GameEnd", f.GD.level));
            }));
    }, G.prototype.quitGame = function() {
        this.model = new s.default(), this.playTypeList = [], this.state == o.结算 && (this.state = o.其他);
    }, G.prototype.getPlayType = function() {
        y.ActivityManager.getInstance().checkActivity(y.ActivityType.NEWYEAR) ? (0 == f.GD.newYearPlayCount || 5 == r.G.newYearPlayCount || Math.random() < .25 ? (this.playTypeList.push(a.元旦),
                r.G.newYearDropCount = 0, r.G.newYearDropTotal = GA.rand(10, 15), r.G.newYearPlayCount = 1) : r.G.newYearPlayCount++,
            f.GD.newYearPlayCount++) : y.ActivityManager.getInstance().checkActivity(y.ActivityType.CHRISTMAS) && (0 == f.GD.christmasPlayCount || 5 == r.G.christmasPlayCount || Math.random() < .25 ? (this.playTypeList.push(a.圣诞),
            r.G.christmasPlayCount = 1) : r.G.christmasPlayCount++, f.GD.christmasPlayCount++);
    }, G.prototype.getBg = function() {
        return "texture/map/bg/" + GA.rand(0, 4);
    }, G.prototype.getJson = function() {
        return this.matchType == i.三对三 ? "json/df_" + GA.rand(1, 3) : "json/5_" + GA.rand(1, 4);
    }, G.prototype.getMap = function(t, e) {
        this.jsonPath = e || this.getJson(), this.bgPath = t || this.getBg();
    }, G.prototype.getLevel = function() {
        var t = 0;
        4 < r.G.wincount ? t = 5 : 3 < r.G.wincount ? t = 4 : 2 < r.G.wincount ? t = 3 : 1 < r.G.wincount ? t = 2 : 0 < r.G.wincount && (t = 1),
            t > this.level && (cc.log("level", t), this.level = t);
    }, G.prototype.initAI = function(t) {
        var n = this;
        this.model.ais = [];
        for (var e, a = this.eggGun, i = 0, o = 0; o < r.G.personModelList.length; o++) r.G.personModelList[o].id == f.GD.personIdx && (i = o);
        switch (t) {
            case 5:
                for (o = Math.max(0, f.GD.topRank - 6); o < Math.min(f.GD.topRank + 12, r.G.gunModelList.length); o++) a.push(o);
                for (; a.length < G.getInstance().aicount;) a.push(GA.rand(f.GD.topRank - 6, f.GD.topRank + 12));
                for (a.sort(function() {
                        return Math.random() - .5;
                    }), o = 0; o < G.getInstance().aicount; o++)(e = new u.default()).openid = r.G.getUuid(),
                    e.personIdx = this.getPersonIdx(GA.rand(i - 6, i + 12)), e.gunIdx = this.getGunIdx(a.shift()),
                    e.think = .1, this.model.ais.push(e);
                break;

            case 4:
                for (o = Math.max(0, f.GD.topRank - 6); o < Math.min(f.GD.topRank + 9, r.G.gunModelList.length); o++) a.push(o);
                for (; a.length < G.getInstance().aicount;) a.push(GA.rand(f.GD.topRank - 6, f.GD.topRank + 9));
                for (a.sort(function() {
                        return Math.random() - .5;
                    }), o = 0; o < G.getInstance().aicount; o++)(e = new u.default()).openid = r.G.getUuid(),
                    e.personIdx = this.getPersonIdx(GA.rand(i - 6, i + 9)), e.gunIdx = this.getGunIdx(a.shift()),
                    e.think = 1 < o ? .3 : .1, this.model.ais.push(e);
                break;

            case 3:
                for (o = Math.max(0, f.GD.topRank - 6); o < Math.min(f.GD.topRank + 6, r.G.gunModelList.length); o++) a.push(o);
                for (; a.length < G.getInstance().aicount;) a.push(GA.rand(f.GD.topRank - 6, f.GD.topRank + 6));
                for (a.sort(function() {
                        return Math.random() - .5;
                    }), o = 0; o < G.getInstance().aicount; o++)(e = new u.default()).openid = r.G.getUuid(),
                    e.personIdx = this.getPersonIdx(GA.rand(i - 6, i + 6)), e.gunIdx = this.getGunIdx(a.shift()),
                    e.think = 1 < o ? .7 : .3, this.model.ais.push(e);
                break;

            case 2:
                for (o = Math.max(0, f.GD.topRank - 6); o < Math.min(f.GD.topRank + 6, r.G.gunModelList.length); o++) a.push(o);
                for (; a.length < G.getInstance().aicount;) a.push(GA.rand(f.GD.topRank - 6, f.GD.topRank + 6));
                for (a.sort(function() {
                        return Math.random() - .5;
                    }), o = 0; o < G.getInstance().aicount; o++)(e = new u.default()).openid = r.G.getUuid(),
                    e.personIdx = this.getPersonIdx(GA.rand(i - 6, i + 6)), e.gunIdx = this.getGunIdx(a.shift()),
                    e.think = 1 < o ? 1.5 : 0 < o ? .7 : .3, this.model.ais.push(e);
                break;

            case 1:
                for (o = Math.max(0, f.GD.topRank - 6); o < Math.min(f.GD.topRank + 3, r.G.gunModelList.length); o++) a.push(o);
                for (; a.length < G.getInstance().aicount;) a.push(GA.rand(f.GD.topRank - 6, f.GD.topRank + 3));
                for (a.sort(function() {
                        return Math.random() - .5;
                    }), o = 0; o < G.getInstance().aicount; o++)(e = new u.default()).openid = r.G.getUuid(),
                    e.personIdx = this.getPersonIdx(GA.rand(i - 6, i + 3)), e.gunIdx = this.getGunIdx(a.shift()),
                    e.think = 0 < o ? 1.5 : .7, this.model.ais.push(e);
                break;

            default:
                for (o = Math.max(0, f.GD.topRank - 6); o < Math.min(f.GD.topRank + 3, r.G.gunModelList.length); o++) a.push(o);
                for (; a.length < G.getInstance().aicount;) a.push(GA.rand(Math.min(f.GD.topRank - 6, 0), f.GD.topRank + 3));
                for (a.sort(function() {
                        return Math.random() - .5;
                    }), o = 0; o < G.getInstance().aicount; o++)(e = new u.default()).openid = r.G.getUuid(),
                    e.personIdx = this.getPersonIdx(GA.rand(i - 6, i + 3)), e.gunIdx = this.getGunIdx(a.shift()),
                    e.think = 1.5, this.model.ais.push(e);
        }
        this.initOther(), this.model.ais.forEach(function(t) {
            var e;
            GA.rand(0, 100) < n.equipscale && (e = GA.rand(n.equipminlv, n.equipmaxlv), t.headEquipIdx = g.default.getInstance().getRandHeadEquipIdx(),
                t.equipDataList.push({
                    id: t.headEquipIdx,
                    lv: e,
                    form: Math.min(2, Math.floor(e / 10)),
                    has: 1
                })), GA.rand(0, 100) < n.equipscale && (e = GA.rand(n.equipminlv, n.equipmaxlv),
                t.backEquipIdx = g.default.getInstance().getRandBackEquipIdx(), t.equipDataList.push({
                    id: t.backEquipIdx,
                    lv: e,
                    form: Math.min(2, Math.floor(e / 10)),
                    has: 1
                }));
        });
    }, G.prototype.initBattleAI = function(t) {
        var n = this;
        this.model.ais = [];
        var e = this.eggGun,
            a = {
                r: 4,
                min: .8,
                max: 1.2
            },
            i = {
                r: 4,
                min: .8,
                max: 1.2
            },
            o = Math.max(0, Math.floor(f.GD.level / 3));
        h.GC.selfAiConfig.forEach(function(t) {
            t.r <= o && (a = t);
        }), h.GC.otherAiConfig.forEach(function(t) {
            t.r <= o && (i = t);
        });
        for (var s = Math.max(0, f.GD.topRank - 3); s < Math.min(f.GD.topRank + 3 * (t + 1), r.G.gunModelList.length); s++) e.push(s);
        for (; e.length < this.aicount;) e.push(GA.rand(f.GD.topRank - 3, f.GD.topRank + 3 * (t + 1)));
        for (var e = this.shuffle(e), l = 0, s = 0; s < r.G.personModelList.length; s++) r.G.personModelList[s].id == f.GD.personIdx && (l = s);
        for (s = 0; s < this.aicount; s++) {
            var c = new u.default();
            c.openid = r.G.getUuid(), c.gunIdx = this.getGunIdx(e.shift()), s < 2 ? (c.personIdx = this.getPersonIdx(GA.rand(l - 3, l + 3)),
                c.think = GA.randFloat(a.min, a.max)) : (c.personIdx = this.getPersonIdx(GA.rand(l - 3, l + 3 * (t + 1))),
                c.think = Math.max(.1, GA.randFloat(i.min, i.max) - .1 * t)), this.model.ais.push(c);
        }
        this.initOther(), this.model.ais.forEach(function(t) {
            var e;
            GA.rand(0, 100) < n.equipscale && (e = GA.rand(n.equipminlv, n.equipmaxlv), t.headEquipIdx = g.default.getInstance().getRandHeadEquipIdx(),
                t.equipDataList.push({
                    id: t.headEquipIdx,
                    lv: e,
                    form: Math.min(2, Math.floor(e / 10)),
                    has: 1
                })), GA.rand(0, 100) < n.equipscale && (e = GA.rand(n.equipminlv, n.equipmaxlv),
                t.backEquipIdx = g.default.getInstance().getRandBackEquipIdx(), t.equipDataList.push({
                    id: t.backEquipIdx,
                    lv: e,
                    form: Math.min(2, Math.floor(e / 10)),
                    has: 1
                }));
        });
    }, G.prototype.initOther = function() {
        for (var t, a = this, e = h.GC.petModelList.map(function(t, e) {
                return e;
            }), i = []; i.length < this.petcount;) 0 < e.length ? (t = GA.rand(0, e.length - 1),
            i.push(e[t]), e.splice(t, 1)) : i.push(GA.rand(0, h.GC.petModelList.length - 1));
        for (; i.length < this.model.ais.length;) i.push(-1);
        for (var i = this.shuffle(i), o = []; o.length < this.machinecount;) o.push(0);
        for (; o.length < this.model.ais.length;) o.push(-1);
        o = this.shuffle(o), this.model.ais.forEach(function(t, e) {
            var n;
            e < i.length && (-1 < (n = i[e]) && Math.random() < a.petscale ? t.petIdx = n : t.petIdx = -1),
                e < o.length && (-1 < (e = o[e]) && Math.random() < a.machinescale ? t.machineIdx = e : t.machineIdx = -1);
        });
    }, G.prototype.shuffle = function(t) {
        for (var e, n, a = t.length; a;) n = Math.floor(Math.random() * a--), e = t[a],
            t[a] = t[n], t[n] = e;
        return t;
    }, G.prototype.getPersonIdx = function(t) {
        return ((t = t < 0 ? 0 : t) >= r.G.personModelList.length ? r.G.personModelList[r.G.personModelList.length - Math.floor(3 * Math.random()) - 1] : r.G.personModelList[t]).id;
    }, G.prototype.getGunIdx = function(t) {
        return 1e3 <= t ? t - 1e3 >= r.G.eggGunModelList.length && (t = r.G.eggGunModelList.length - Math.floor(3 * Math.random()) + 999) : t >= r.G.gunModelList.length && (t = r.G.gunModelList.length - Math.floor(3 * Math.random()) - 1),
            t = t < 0 ? 0 : t;
    }, G.prototype.checkCameraDistance = function(t, e, n) {
        return void 0 === e && (e = 160), void 0 === n && (n = this.gameView && this.gameView.followCamera ? this.gameView.followCamera.zoomRatio : 1),
            this.gameView && this.gameView.followCamera && (Math.abs(t.x - this.gameView.followCamera.node.x) > (cc.winSize.width + e >> 1) / n || Math.abs(t.y - this.gameView.followCamera.node.y) > (cc.winSize.height + e >> 1) / n);
    }, G.prototype.pause = function() {
        if (this.gameView) {
            this.gameView.pause();
            try {
                d.default.getInstance().pause();
            } catch (t) {}
        }
    }, G.prototype.resume = function() {
        if (this.gameView) {
            this.gameView.resume();
            try {
                d.default.getInstance().resume();
            } catch (t) {}
        }
    }, G._manager = null, G;
}();

n.default = P;